﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Compilex.Syntatic.Declarations
{
    public class IfBlock
    {
        public SyntaticContext Context { get; set; }
        public IfBlock(SyntaticContext context)
        {
            this.Context = context;
        }

        // depois da expressao
        public void Start()
        {
            BooleanExpression condition = Context.ExpressionStack.JustPopped;
            if (condition.Type.TypeValue != TypeValue.boolex)
            {
                throw new SyntaticException("Expressões '" + condition.Type + "' não podem ser usadas como condição.");
            }
            Context.CommandBlockStack.Current.CodeGenerator.StartIf();
            Context.ScopeManager.OpenScope();
        }

        // 
        public void Else()
        {
            Context.ScopeManager.CloseScope();
            Context.ScopeManager.OpenScope();
            Context.CommandBlockStack.Current.CodeGenerator.Else();
        }

        // finex
        public void End()
        {
            Context.CommandBlockStack.Current.CodeGenerator.EndIf();
            Context.ScopeManager.CloseScope();
        }
    }
}
